package com.example.dakotanews.controller;

import com.example.dakotanews.dto.CommitCommentDTO;
import com.example.dakotanews.dto.utils.ApiResponse;
import com.example.dakotanews.dto.utils.ResponseCode;
import com.example.dakotanews.service.CommentService;
import com.example.dakotanews.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/comment")
public class CommentController {

    private final CommentService commentService;
    private final JwtUtil jwtUtil;

    // 使用构造函数进行自动装配
    @Autowired
    public CommentController(
            CommentService commentService,
            JwtUtil jwtUtil
    ) {
        this.commentService = commentService;
        this.jwtUtil = jwtUtil;
    }

    /**
     * @description 根据新闻ID获取新闻下的全部评论 按照指定顺序排序 并分页
     * @param sortType （必需）排序方式，详见接口文档
     * @param page （可选）起始页码
     * @param pageSize （可选）页面大小
     * @return 返回对应评论封装成的JSON
     */
    @GetMapping("/getNewsComments")
    public ApiResponse<Object> getNewsComments(
            @RequestParam Integer newsId,
            @RequestParam Integer sortType,
            @RequestParam(defaultValue = "1")int page,
            @RequestParam(defaultValue = "10")int pageSize,
            @RequestHeader(value = "Authorization", required = false)String jwt) {

        Integer userId = jwtUtil.extractUserIdFromJWT(jwt);

        return commentService.getNewsAllComment(newsId, sortType, page, pageSize, userId);
    }

    /**
     * @description 发表评论接口 包含发表评论、回复评论、回复二级评论的功能业务逻辑
     * @param commitCommentDTO 请求体，包含以下成员属性
     * newsId （必需）新闻ID
     * parentId （可选）父评论ID
     * repliedUserId （可选）回复用户ID
     * comment （必需）评论内容
     * @param jwt （可选）用于解析发评论的用户ID
     * @return 返回新增记录的ID
     */
    @PostMapping("/commit")
    public ApiResponse<Object> commitNewsComment(
            @RequestBody CommitCommentDTO commitCommentDTO,
            @RequestHeader(value = "Authorization", required = false)String jwt) {

        Integer userId = jwtUtil.extractUserIdFromJWT(jwt);
        if (userId == null) {
            return new ApiResponse<>(ResponseCode.UNAUTHORIZED, "未授权的请求", "请先登录");
        }

        Integer newsId = commitCommentDTO.getNewsId();
        Integer parentId = commitCommentDTO.getParentId();
        Integer repliedUserId = commitCommentDTO.getRepliedUserId();
        String comment = commitCommentDTO.getComment();

        if(parentId == null){
            return commentService.commitComment(newsId, comment, userId);
        } else {
            return commentService.commitChildComment(newsId, parentId, repliedUserId, comment, userId);
        }

    }

    /**
     * @description 评论点赞接口（包含一级评论和二级评论），实际业务中是两种参数选其一进行传递
     * @param commentId （可选）一级评论ID
     * @param childCommentId （可选）二级评论ID
     * @return 操作结果
     */
    @GetMapping("/support")
    public ApiResponse<Object> supportComment(
            @RequestParam (required = false)Integer commentId,
            @RequestParam (required = false)Integer childCommentId,
            @RequestHeader(value = "Authorization", required = false)String jwt) {

        Integer userId = jwtUtil.extractUserIdFromJWT(jwt);
        if (userId == null) {
            return new ApiResponse<>(ResponseCode.UNAUTHORIZED, "未授权的请求", "请先登录");
        }

        if(commentId != null && childCommentId == null) {
            // 如果传递的是一级评论ID
            return commentService.supportComment(commentId, userId);
        } else if(childCommentId != null && commentId == null) {
            // 如果传递的是二级评论ID
            return commentService.supportChildComment(childCommentId, userId);
        } else {
            return new ApiResponse<>(ResponseCode.BAD_REQUEST, "未知参数", "无法识别操作类型");
        }

    }

}
